package com.feifan.monthhelper.utils;

import java.math.BigDecimal;

public class NumberUtils {

    static final int DEFAULT_NUMBER = -99;

    /**
     * 将 double 转 String
     *
     * @param number   double
     * @param newScale 小数位数
     */
    public static String toString(double number, int newScale) {
        BigDecimal bd = new BigDecimal(number);
        bd = bd.setScale(newScale, BigDecimal.ROUND_FLOOR);
        return bd.toString();
    }

    /**
     * 将 String 转 int
     *
     * @param number   String
     */
    public static int StringToInt(String number) {
        return StringToInt(number, DEFAULT_NUMBER);
    }

    /**
     * 将 String 转 int
     *
     * @param number   String
     * @param defaultNumber   int
     */
    public static int StringToInt(String number, int defaultNumber) {
        try {
            return Integer.parseInt(number);
        } catch (Exception e) {
            return defaultNumber;
        }
    }
}
